//共阴数码管驱动,com共极,seg段极
//  com输出L,seg输出H,数码管显示有效

#include "App_Main.h"
#include <math.h>


UI_System_S UI_System;
uint16_t Ver_Enable = 0;
uint16_t Shutdown_CountDown = 0;


void UI_Show(int32_t temp)
{
    static uint8_t seq;
    uint8_t show_H1 = 0;

    UI_System.UI_Show_Colon = UI_OFF;
    UI_System.ShutdownCount = 0;
    if((Switch_State == Temp_Shutoff) && (ExhaustFanClock == 0))
    {
        temp = Tube_Encoder_None;
        temp <<= 8;
        temp |= Tube_Encoder_None;
        temp <<= 8;
        temp |= Tube_Encoder_None;
        if(Ver_Enable == 0) NT_CharsSettings(temp);

        return;
    }

    if((RTD[0].status == Pt_Normal) && (ExhaustFanClock == 0))
    { //只在铂电阻正常情况下 有效
        if((switch_trigger_Flag > 0) && (temp_set != Temp_Set_Smoke))
        {
            temp = temp_set;
            if(temp < HighTempSetting) show_H1 = 0;
            else show_H1 = 1;
            if(UI_System.Grill_Unit == Unit_C)
            {
                temp = F_Convert_C(temp);
            } 
            Keyboard_Show_Count = 0;
            Keyboard1_Show_Count = 0;
        }
        if(show_H1 == 0) NT_NumsSettings(temp);
        else
        {
            temp = Tube_Encoder_H;
            temp <<= 8;
            temp |= 1;
            temp <<= 8;
            temp |= Tube_Encoder_None;
            NT_CharsSettings(temp);
        }
        if(Keyboard_Show_Count > 0)
        {
            Keyboard1_Show_Count = 0;
            temp = Tube_Encoder_P;
            temp <<= 8;
            temp |= Tube_Encoder_Dash;
            temp <<= 8;
            temp |= UI_System.Pset - PSet_0;
            NT_CharsSettings(temp);
        }
        if(Keyboard1_Show_Count > 0)
        {
            Keyboard_Show_Count = 0;
            temp = Tube_Encoder_P;
            temp <<= 8;
            temp |= 0;
            temp <<= 8;
            temp |= key_mset_state;
            NT_CharsSettings(temp);
        }
        if(Shutdown_CountDown > 0) Shutdown_CountDown = 0;
    }
    else
    {
        if(Temp_Param.oven_Err != Pt_Normal)
        {
            if(Temp_Param.oven_Err == Pt_Open)
            {
                temp = Tube_Encoder_E;
                temp <<= 8;
                temp |= Tube_Encoder_r;
                temp <<= 8;
                temp |= Tube_Encoder_O;
                NT_CharsSettings(temp);
            }
            else if(Temp_Param.oven_Err == Pt_Short)
            {
                temp = Tube_Encoder_E;
                temp <<= 8;
                temp |= Tube_Encoder_r;
                temp <<= 8;
                temp |= Tube_Encoder_S;
                NT_CharsSettings(temp);
            }
            else if(Temp_Param.oven_Err == Pt_LowTemp)
            {
                temp = Tube_Encoder_E;
                temp <<= 8;
                temp |= Tube_Encoder_r;
                temp <<= 8;
                temp |= Tube_Encoder_L;
                NT_CharsSettings(temp);
            }
            else if(Temp_Param.oven_Err == Pt_HighTemp)
            {
                temp = Tube_Encoder_E;
                temp <<= 8;
                temp |= Tube_Encoder_r;
                temp <<= 8;
                temp |= Tube_Encoder_H;
                NT_CharsSettings(temp);
            }
            if(Shutdown_CountDown > 0) Shutdown_CountDown = 0;
        }
        else
        {
            if(Shutdown_CountDown == 0) Shutdown_CountDown = 1;
            if(Shutdown_CountDown <= 50)
            { //10ms * 50
                temp = Tube_Encoder_O;
                temp <<= 8;
                temp |= Tube_Encoder_F;
                temp <<= 8;
                temp |= Tube_Encoder_F;
                NT_CharsSettings(temp);
            }
            else if(Shutdown_CountDown <= 100)
            {
                temp = Tube_Encoder_None;
                temp <<= 8;
                temp |= Tube_Encoder_None;
                temp <<= 8;
                temp |= Tube_Encoder_None;
                NT_CharsSettings(temp);               
            }
            else
            {
                if (ExhaustFanClock > FanClose_CountDown)
                { //显示OFF, > 600s
                    Shutdown_CountDown = 0;
                }
                else if (ExhaustFanClock > 0)
                { //关机倒计时
                    UI_System.ShutdownCount = 1;
                    UI_System.UI_Show_Colon = UI_ON;
                    if(ExhaustFanClock >= 600) ExhaustFanClock = 599;
                    temp = ExhaustFanClock / 60; //时间 转 min
                    temp *= 100;
                    temp += (ExhaustFanClock % 60); //时间 转 s

                    NT_NumsSettings(temp);
                }
            }
        }
    }
}

void UI_Init(void)
{
    UI_System.Grill_Unit = Unit_F;
    ExhaustFanClock = 0;
    UI_System.UI_Show_Colon = UI_OFF;
    UI_System.ShutdownCount = 0;
}

//版本号格式：vFXXXX 华氏度,版本XX
//           vCXXXX 摄氏度,版本XX
//Tick 1s
void UI_Ver(void)
{
    uint32_t temp;
    uint32_t ver = 33;
    static uint16_t ver_clock = 0;

    if(Switch_State != Temp_Shutoff) return;
    if(key_param.once1 == 0)
    {
        if(Ver_Enable > 0)
        {
            Com0_H;
            Com1_H;
            Com2_H;
            Com3_H;
            ver_clock = 0;
            Ver_Enable = 0;
        }
        return;
    }

    Ver_Enable = 1;
    if(ver_clock == 0)
    {
        temp = Tube_Encoder_v; //v
    }
    else if(ver_clock == 1)
    {
        temp = Tube_Encoder_v; //v
        temp <<= 8;
        if(UI_System.Grill_Unit == Unit_F) temp |= Tube_Encoder_F; //F
        else temp |= Tube_Encoder_C; //C
    }
    else if(ver_clock == 2)
    {
        temp = Tube_Encoder_v; //v
        temp <<= 8;
        if(UI_System.Grill_Unit == Unit_F) temp |= Tube_Encoder_F; //F
        else temp |= Tube_Encoder_C; //C
        temp <<= 8;
        temp |= (ver/10) % 10; //X
    }
    else if(ver_clock == 3)
    {
        if(UI_System.Grill_Unit == Unit_F) temp = Tube_Encoder_F; //F
        else temp = Tube_Encoder_C; //C
        temp <<= 8;
        temp |= (ver/10) % 10;
        temp <<= 8;
        temp |= (ver/1) % 10;
    }
    else if(ver_clock == 4)
    {
        temp = (ver/10) % 10;
        temp <<= 8;
        temp |= (ver/1) % 10;
        temp <<= 8;
        temp |= Tube_Encoder_None;
    }
    else if(ver_clock == 5)
    {
        temp = (ver/1) % 10;
        temp <<= 8;
        temp |= Tube_Encoder_None;
        temp <<= 8;
        temp |= Tube_Encoder_None;
    }
    ver_clock++;
    if(ver_clock >= 6) ver_clock = 0;
    NT_Shine_Stop();
    NT_CharsSettings(temp);
}
